package com.microservice.common.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Properties;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

/**
 * 邮件测试工具类
 * 用于测试邮件配置是否正确
 */
@Slf4j
@Component
public class MailTestUtil {
    
    /**
     * 测试邮件连接
     * 
     * @param host SMTP服务器
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param useSSL 是否使用SSL
     * @return 测试结果
     */
    public boolean testConnection(String host, int port, String username, String password, boolean useSSL) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", String.valueOf(port));
        props.put("mail.smtp.auth", "true");
        
        if (useSSL) {
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.trust", host);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", String.valueOf(port));
        } else {
            props.put("mail.smtp.starttls.enable", "true");
        }
        
        props.put("mail.smtp.connectiontimeout", "10000");
        props.put("mail.smtp.timeout", "10000");
        
        try {
            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            
            Transport transport = session.getTransport("smtp");
            transport.connect(host, port, username, password);
            transport.close();
            
            log.info("✅ 邮件连接测试成功: {}:{}", host, port);
            return true;
        } catch (Exception e) {
            log.error("❌ 邮件连接测试失败: {}:{}, error={}", host, port, e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 发送测试邮件
     */
    public boolean sendTestMail(String host, int port, String username, String password, 
                                boolean useSSL, String to) {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.port", String.valueOf(port));
        props.put("mail.smtp.auth", "true");
        
        if (useSSL) {
            props.put("mail.smtp.ssl.enable", "true");
            props.put("mail.smtp.ssl.trust", host);
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", String.valueOf(port));
        } else {
            props.put("mail.smtp.starttls.enable", "true");
        }
        
        props.put("mail.smtp.connectiontimeout", "10000");
        props.put("mail.smtp.timeout", "10000");
        
        try {
            Session session = Session.getInstance(props, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });
            
            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(username));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
            message.setSubject("测试邮件");
            message.setText("这是一封测试邮件，如果您收到此邮件，说明邮件配置正确！");
            
            Transport.send(message);
            
            log.info("✅ 测试邮件发送成功: {} -> {}", username, to);
            return true;
        } catch (Exception e) {
            log.error("❌ 测试邮件发送失败: error={}", e.getMessage());
            e.printStackTrace();
            return false;
        }
    }
}
